package xyz.yuzegod.megawalls.classes.normal.creeper;

import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("引爆", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 6.0;
            }
            case 2: {
                return 7.0;
            }
            case 3: {
                return 8.0;
            }
            case 4: {
                return 9.0;
            }
            case 5: {
                return 10.0;
            }
            default: {
                return 6.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7引发一场爆炸,");
            lore.add("   §7对周围敌人造成至多§a" + this.getAttribute(level) + "§7点伤害");
            lore.add("   §7不过,需要3秒才能引爆。");
            return lore;
        }
        lore.add("   §7对周围敌人造成至多§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7点伤害");
        lore.add("   §7不过,需要3秒才能引爆。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        player.getWorld().playSound(player.getLocation(), Sound.WOOD_CLICK, 1.0f, 0.0f);
        new BukkitRunnable() {
            int step = 19;
            int countdown = 4;
            
            public void run() {
                if (!player.isOnline()) {
                    this.cancel();
                    return;
                }
                ParticleEffect.SMOKE_NORMAL.display(0.1f, 0.5f, 0.1f, 0.75f, 2, player.getLocation().add(0.0, 1.0, 0.0), 10.0);
                if (MegaWalls.getRandom().nextInt(100) <= 20) {
                    ParticleEffect.VILLAGER_ANGRY.display(0.1f, 0.4f, 0.1f, 1.0f, 1, player.getLocation().add(0.0, 1.0, 0.0), 10.0);
                }
                ++this.step;
                if (this.step == 20 && this.countdown > 0) {
                    this.step = 0;
                    --this.countdown;
                    ParticleEffect.EXPLOSION_LARGE.display(0.0f, 0.0f, 0.0f, 0.3f, 3, player.getLocation().add(0.0, 1.0, 0.0), 10.0);
                    ParticleEffect.EXPLOSION_NORMAL.display(0.0f, 0.5f, 0.0f, 1.0f, 20, player.getLocation().add(0.0, 1.0, 0.0), 10.0);
                    player.getWorld().playSound(player.getLocation(), Sound.CREEPER_HISS, 1.0f, 2.0f);
                }
                else if (this.countdown == 0) {
                    ParticleEffect.EXPLOSION_HUGE.display(0.0f, 0.0f, 0.0f, 1.0f, 1, player.getLocation().add(0.0, 1.0, 0.0), 10.0);
                    player.getWorld().playSound(player.getLocation(), Sound.EXPLODE, 1.0f, 0.5f);
                    player.sendMessage("§a§l轰隆！");
                    for (final Player player1 : MainSkill.this.getNearbyPlayers(player.getLocation(), player, 6)) {
                        player1.damage((double)MegaWalls.getRandom().nextInt(5 + kitStats.getSkillLevel()), (Entity)player);
                    }
                    this.cancel();
                }
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        return true;
    }
    
    private List<Player> getNearbyPlayers(final Location location, final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers(location, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator() && !gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(location) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
